package com.sx.mvvmlibs.utils

import android.os.Environment
import com.sx.mvvmlibs.base.BaseApp
import java.io.File

/**
 * @Author: YY
 * @Date: 2022/1/5
 * @Describe: 文件工具类
 */
object FileTools {

    // 取得App的log目录：/sdcard/包名/Log，没有sd卡则是/data/data/包名/files/Log
    val appLogDir: String
        get() {
            val s = "/Log/"
            val path = if (isSDCardMount) {
                cacheDir.absolutePath + s
            } else {
                BaseApp.getInstance().filesDir.absolutePath + s
            }
            val file = File(path)
            if (!file.exists()) {
                file.mkdirs()
            }
            return path
        }

    // SD卡是否被挂载
    private val isSDCardMount: Boolean
        get() = Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED

    // 缓存目录
    private val cacheDir: File
        get() {
            return BaseApp.getInstance().externalCacheDir ?: return BaseApp.getInstance().cacheDir
        }

}